package com.example.demo.controller;

import com.example.demo.entity.Acc;
import com.example.demo.entity.ReturnT;
import com.example.demo.repository.AccRepository;
import com.example.demo.repository.ToolDAO;
import com.example.demo.util.codeGen.CodeGen;
import com.example.demo.util.codeGen.ColumnInfo;
import com.sun.xml.txw2.output.ResultFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mqp
 * @description acc
 * @date 2021-12-20
 */
@RestController
@RequestMapping("/all")
@Slf4j
@Api(tags = "all")
@Transactional
@CrossOrigin
public class AllController {

    @Resource
    ToolDAO toolDAO;
    @PostMapping("/genCode")
    public Object genCode() {
//        userService.editUser(requestUser);
//        String controllerPackageName = LibraryController.class.getPackage().getName();
//        log.info("controllerPackageName {}",controllerPackageName);


//        EntityManager entityManager;
//        entityManager.find()
//        <expression> or DISTINCT expected, got '*'
//        String hql="Select * From User";
//        List<User> userList=entityManager.createQuery(hql);
//        Query nativeQuery = entityManager.createNativeQuery(hql);
//        List resultList = nativeQuery.getResultList();
//        nativeQuery.setParameter()
        //        List<Map<String, Object>> maps = userDAO.selectTableNames("wj", "base table");
//        List<Map<String, Object>> tableNames =
//                userDAO.selectTableNames("gcsm", "base table");
//        for (Map<String, Object> tableName : tableNames) {
//            tableName.get("ta")
//        }

        String table_schema= "gcsm";
        List<Map<String, Object>> maps =
                toolDAO.selectTableNames("gcsm", "base table");
//        List<TableName> tableNames = userDAO.selectTableNamesToObj("gcsm", "base table");
        System.out.println(maps);
        log.info("selectTableNames");
        log.info("maps {}",maps);

//        userDAO.saveAll()

//        CodeGen
//        File file = new File("repo");

        Set<String> set=new HashSet<>();
        List<String> table_names = maps.stream().map(o -> {
            String table_name = (String) o.get("table_name");
            return table_name;
        }).collect(Collectors.toList());
        Map<String ,List<ColumnInfo>>mapTableCols=new HashMap<>();
        for (String table_name : table_names) {
            List<Map<String, Object>> tableCols =
                    toolDAO.selectTableCols(table_schema, table_name);
            List<ColumnInfo> columnInfos = tableCols.stream().map(o -> {
                ColumnInfo columnInfo = new ColumnInfo();
                columnInfo.fromMap(o);
                return columnInfo;
            }).collect(Collectors.toList());
            mapTableCols.put(table_name,columnInfos);

            List<String> dataTypes = columnInfos.stream()
                    .map(o -> o.getDATA_TYPE()).collect(Collectors.toList());
            set.addAll(dataTypes);
        }
        System.out.println("mapTableCols");
        System.out.println(mapTableCols);
        CodeGen.gen( mapTableCols);

//        columnInfos.

        System.out.println("set 字段类型有哪些");
        System.out.println(set);
//        set 字段类型有哪些
//[longtext, datetime, double, varchar, char, tinyint, int]
//        CodeGen

//        maps.forEach(o->{
//            TableName tableName = new TableName();
//            tableName.fromMap(o);
//            return
//        });
//        List<TableName> tableNames = maps.stream().map(o -> {
//            TableName tableName = new TableName();
//            tableName.fromMap(o);
//            return tableName;
//        }).collect(Collectors.toList());
//        log.info("tableNames {}",tableNames);
//        log.info(tableNames);

//        Co

        return  ReturnT.success(mapTableCols);
//        Result.
//        return ResultFactory.buildSuccessResult(mapTableCols);
    }

}
